package com.example.flex.controller;

import com.example.flex.domain.UserEntity;
import com.example.flex.service.UserService;
import com.example.flex.utils.common.PageEntity;
import com.example.flex.utils.response.BaseResponse;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 用户表控制层
 * @Author: 不是菜鸡爱编程
 * @Date:
 * @Description：
 */
@RestController
@Api(tags = "用户表接口")
@RequestMapping("user")
public class UserController{

    @Autowired
    private UserService userService;

    /**
     * 登录
     *
     * @param userEntity 用户实体
     * @return {@link BaseResponse}<{@link ?}>
     */
    @PostMapping("/login")
    @ApiOperation("登录验证")
    public BaseResponse<?> login(@RequestBody @ApiParam("用户表") UserEntity userEntity){
        return userService.login(userEntity);
    }

    /**
     * 保存
     *
     * @param userEntity 用户表
     * @return BaseResponse
     */
    @PostMapping("/save")
    @ApiOperation("保存用户表")
    public BaseResponse<?> save(@RequestBody @ApiParam("用户表") UserEntity userEntity){
        boolean saveFlag = userService.save(userEntity);
        if(saveFlag){
            return BaseResponse.success("保存成功");
        }else{
            return BaseResponse.error("保存失败");
        }
    }

    /**
     * 根据主键查询
     *
     * @param id
     * @return BaseResponse
     */
    @GetMapping("/info/{id}")
    @ApiOperation("根据主键查询用户表")
    public BaseResponse<?> info(@PathVariable @ApiParam("用户表主键") Integer id){
        return BaseResponse.success(userService.getById(id),"查询成功");
    }

    /**
     * 列表查询
     *
     * @return BaseResponse
     */
    @PostMapping("/list")
    @ApiOperation("列表查询用户表")
    public BaseResponse<?> list(@RequestBody @ApiParam("用户表分页参数") PageEntity<UserEntity> userEntityPage){
        UserEntity userEntity = userEntityPage.getData();
        QueryWrapper queryWrapper = getQueryWrapper(userEntity);
        return BaseResponse.success(userService.page(new Page<>(userEntityPage.getPageNum(),userEntityPage.getPageSize()),queryWrapper),"查询成功");
    }

    /**
     * 删除
     *
     * @param ids
     * @return BaseResponse
     */
    @DeleteMapping("/delete/{ids}")
    @ApiOperation("删除用户表")
    public BaseResponse<?> delete(@PathVariable @ApiParam("用户表主键(列表)") Integer[] ids){
        return BaseResponse.success(userService.removeByIds(Arrays.asList(ids)),"删除成功");
    }


    /**
     * 修改
     *
     * @param userEntity 用户表
     * @return BaseResponse
     */
    @PutMapping("/edit")
    @ApiOperation("修改用户表")
    public BaseResponse<?> edit(@RequestBody @ApiParam("用户表") UserEntity userEntity){
        return BaseResponse.success(userService.updateById(userEntity),"修改成功");
    }

    /**
     * 获取queryWrapper
     *
     * @param userEntity 用户表
     * @return {@link QueryWrapper}
     */
    private QueryWrapper getQueryWrapper(UserEntity userEntity){
        return QueryWrapper.create()
                    .eq(UserEntity::getId,userEntity.getId(),userEntity.getId()!=null)
                    .eq(UserEntity::getUsername,userEntity.getUsername(),userEntity.getUsername()!=null)
                    .eq(UserEntity::getPassword,userEntity.getPassword(),userEntity.getPassword()!=null)
                    .eq(UserEntity::getNickName,userEntity.getNickName(),userEntity.getNickName()!=null)
                    .eq(UserEntity::getGender,userEntity.getGender(),userEntity.getGender()!=null)
                    .eq(UserEntity::getHeader,userEntity.getHeader(),userEntity.getHeader()!=null)
                    .eq(UserEntity::getPhone,userEntity.getPhone(),userEntity.getPhone()!=null)
                    .eq(UserEntity::getEmail,userEntity.getEmail(),userEntity.getEmail()!=null)
                    .eq(UserEntity::getStatus,userEntity.getStatus(),userEntity.getStatus()!=null)
                    .eq(UserEntity::getRemark,userEntity.getRemark(),userEntity.getRemark()!=null)
                    .eq(UserEntity::getLoginDate,userEntity.getLoginDate(),userEntity.getLoginDate()!=null)
                    .eq(UserEntity::getCreateId,userEntity.getCreateId(),userEntity.getCreateId()!=null)
                    .eq(UserEntity::getCreateTime,userEntity.getCreateTime(),userEntity.getCreateTime()!=null)
                    .eq(UserEntity::getUpdateId,userEntity.getUpdateId(),userEntity.getUpdateId()!=null)
                    .eq(UserEntity::getUpdateTime,userEntity.getUpdateTime(),userEntity.getUpdateTime()!=null)
                    .eq(UserEntity::getDelFlag,userEntity.getDelFlag(),userEntity.getDelFlag()!=null);
    }
}